import { configureStore } from "@reduxjs/toolkit";
import cartReducer from "./cart";
import wrongReducer from './wrong';
import collectReducer from './collect';

// 从 localStorage 获取持久化的状态
const persistedWrongState = localStorage.getItem('wrongQuestions')
  ? { wrongQuestions: JSON.parse(localStorage.getItem('wrongQuestions')) }
  : undefined;

export const store = configureStore({
  reducer: {
    cart: cartReducer,
    wrong: wrongReducer,
    collect: collectReducer
  },
  preloadedState: {
    wrong: persistedWrongState
  }
});

// 订阅 store 变化，保存到 localStorage
store.subscribe(() => {
  const state = store.getState();
  localStorage.setItem('wrongQuestions', JSON.stringify(state.wrong.wrongQuestions));
});

export default store;
